package com.fangdada.demowebfulx.controller;

import com.fangdada.demowebfulx.entity.User;
import com.fangdada.demowebfulx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author: fd
 * @Date 2021/11/30 10:12
 * @Description: 注解编程模型实现SpringWebflux
 */
@RestController
@Slf4j
public class UserController {

	@Value("#{'${test1:}'.empty ? '${test2:}' : '${test1:}'}")
	private String test;
	
	private final UserService userService;

	public UserController(UserService userService) {
		this.userService = userService;
	}

	//id 查询
	@GetMapping("/user/{id}")
	public Mono<User> getUserId(@PathVariable int id) throws InterruptedException {
		log.info(test);
		Thread.sleep(200000);
		return userService.getUserById(id);
	}

	//查询所有
	@GetMapping("/user")
	public Flux<User> getUsers() {
		return userService.getAllUser();
	}

	//添加
	@PostMapping("/saveUser")
	public Mono<Void> saveUser(@RequestBody User user) {
		Mono<User> userMono = Mono.just(user);
		return userService.saveUserInfo(userMono);
	}

}
